package com.cskaoyan.search.service.impl;

import com.cskaoyan.mall.dto.AllItemServiceResponse;
import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.search.converter.ProductConverter;
import com.cskaoyan.search.entity.ItemDocument;
import com.cskaoyan.search.remote.ProductApiClient;
import com.cskaoyan.search.repository.ProductRepository;
import com.cskaoyan.search.service.InitDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Aurhor: 帅的一批
 * @CreateAt: 2022/11/1 21:50
 * @Description:
 * @PACKAGE_NAME： com.cskaoyan.search.service.impl
 */

@Service
@Slf4j
public class InitDataServiceImpl implements InitDataService {

    @Autowired
    ProductApiClient productApiClient;

    @Autowired
    ProductRepository productRepository;

    @Autowired
    ProductConverter converter;

    @Override
    public void initItems() {
        log.info("elasticSearch开始初始化");
        AllItemServiceResponse allProductItem = productApiClient.getAllProductItem();
        List<ItemDto> callData = allProductItem.getCallData();
        List<ItemDocument> itemDocuments = converter.items2Documnets(callData);
        productRepository.saveAll(itemDocuments);
        log.info("elasticSearch初始化成功");
    }
}
